# Disable in-source builds to prevent source tree corruption.
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
         message(FATAL_ERROR "FATAL: In-source builds are not allowed.
        You should create a separate directory for build files.")
endif()

cmake_minimum_required(VERSION 3.0.0)

if(POLICY CMP0048)
  cmake_policy(SET CMP0048 NEW)
endif()

PROJECT(coap-demo)

SET(CMAKE_BUILD_TYPE Release)

SET(PRO_TOP_PATH "${PROJECT_SOURCE_DIR}")



# # find_library(COAP_LIB coap-2 PATHS ./)
# set(CMAKE_INSTALL_LIBDIR "/data/coap-demo/libs")
# set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_LIBDIR}")
# # 设置链接器标志以启用 RUNPATH
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--enable-new-dtags")


# Specify arm-linux-musleabi host
if (ARM_HOST)
  set(CMAKE_C_COMPILER arm-linux-musleabi-gcc)
  set(CMAKE_CXX_COMPILER arm-linux-musleabi-g++)
endif()

# Specify aarch64-linux-musl host
if (AARCH64_HOST)
  set(CMAKE_C_COMPILER aarch64-linux-musl-gcc)
  set(CMAKE_CXX_COMPILER aarch64-linux-musl-g++)
  # set(CMAKE_INSTALL_LIBDIR "/lib:/data/coap-demo/libs")
  # set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_LIBDIR}")
endif()


# build execute demo
if (ENABLE_DEMO)
  add_executable(coap-server coap-server.c
                coap-resource.c
                coap-server-implement.c)
  target_link_libraries(coap-server ${PRO_TOP_PATH}/libcoap-2.so)

  add_executable(coap-client coap-client.c)
  target_link_libraries(coap-client ${PRO_TOP_PATH}/libcoap-2.so)
endif()


# Overall build libraries
if (COAP_EXTRA_LIB)
  add_library(coapExtra-1 SHARED 
              coap-server.c 
              coap-resource.c
              coap-server-implement.c
              coap-client.c)
  target_include_directories(coapExtra-1 PUBLIC ${PRO_TOP_PATH}/include)

  if (ARM_HOST)
    target_link_libraries(coapExtra-1 
      ${PRO_TOP_PATH}/lib/arm/libcoap-2.so.0.1.0)
  endif()

  if (AARCH64_HOST)
    target_link_libraries(coapExtra-1 
      ${PRO_TOP_PATH}/lib/aarch64/libcoap-2.so.0.1.0)
  endif()

  if (x86_64)
    target_link_libraries(coapExtra-1 
      ${PRO_TOP_PATH}/lib/linux/libcoap-2.so.0.1.0)
  endif()

endif()

# # 设置安装后的 RPATH，并启用链接路径的使用
# set_target_properties(coap-server PROPERTIES
#     INSTALL_RUNPATH "/lib:/data/coap-demo/libs"
#     INSTALL_RPATH_USE_LINK_PATH TRUE
#     # 如果你不希望构建时使用这些路径（只用于安装后），可以设置 SKIP_BUILD_RPATH 为 TRUE
#     # SKIP_BUILD_RPATH TRUE
#     # 如果你希望在构建时就使用安装后的 RPATH（通常用于测试），可以设置 BUILD_WITH_INSTALL_RPATH 为 TRUE
#     # BUILD_WITH_INSTALL_RPATH TRUE
# )
